package com.polimi.meteocal.security;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.polimi.meteocal.business.control.EventManager;
import com.polimi.meteocal.business.control.InviteManager;
import com.polimi.meteocal.business.control.UserManager;
import com.polimi.meteocal.business.entity.Event;
import com.polimi.meteocal.business.entity.User;
import com.polimi.meteocal.business.util.PersonalStringTokenizer;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


@ManagedBean
@SessionScoped
public class EventsCreatedBean implements Serializable {
/*
 * This bean has to goals: 
 * 1.to load the events created by the user (List<Event> eventsCreated) and
 * 2.to take as input the event modified and to store it.
 */
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	EventManager em;
	@EJB
	UserManager um;
	@EJB
	InviteManager im;
	
	//List of events created
	private List<Event> eventsCreated;
	
	

	
	//NewEvent
	private int id;
	private Date date;
	private Date duration;
	private String name;
	private String place;
	private boolean outdoor;
	private boolean publicVisibility;
	private String description;
	
	private String weather;
	private Double temperature;
	
	private String invites;
	private String organizers;
	
	
	@PostConstruct
	public void init(){
		
		//Set the events created by the logged user
		this.eventsCreated= em.getEventsCreated(um.getLoggedUser().getEmail());
		
	
		
	}
	
	public void setEventChoose(ActionEvent event){
		
		//Take the event choosen to be modified and save its parameters
		Event ev=(Event) event.getComponent().getAttributes().get("event");
		
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        sContext.setAttribute("eventChoose", ev);
		
		this.setId(ev.getId());
		this.setDate(ev.getDatetimebegin());
		this.setDuration(ev.getDuration());
		this.setName(ev.getName());
		this.setPlace(ev.getPlace());
		this.setDescription(ev.getDescription());
		this.setOutdoor(ev.getOutdoor());
		this.setPublicVisibility(ev.getPublicvisibility());
		
		
	}
	
public String storeEvent(){
		
		
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        
        //Get the event choosen by the user
        Event oldEvent= (Event) sContext.getAttribute("eventChoose");
		
		
		PersonalStringTokenizer pSt= new PersonalStringTokenizer();
		
		//Creation of usersToInvite
		List<String> usersToInvite= pSt.tokenizeString(this.invites);
				
		//Creation of eventOrganizers
		List<String> eventOrganizers= pSt.tokenizeString(this.organizers);
		
		//Creation of event
		Event event= new Event(); 
		event.setId(oldEvent.getId());
		event.setDatetimebegin(date);
		event.setDuration(duration);
		event.setName(name);
		event.setPlace(place);
		event.setOutdoor(outdoor);
		event.setPublicvisibility(publicVisibility);
		event.setDescription(description);
		
		//Print New Invites
		Iterator<String> it3= usersToInvite.iterator();
		System.out.println("NEW INVITES ");
		while(it3.hasNext()){
				System.out.println("EventsCreatedBean dice : "+it3.next());
		}
		
		this.em.editEvent(event, eventOrganizers);	
		this.im.addInvites(usersToInvite, event);
		return "calendarPage";
	}

	public String delete(){
		FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        ServletContext sContext = request.getSession().getServletContext();
        
        //Get the event choosen by the user
        Event oldEvent= (Event) sContext.getAttribute("eventChoose");
        
        em.deleteEvent(oldEvent);
        return "calendarPage";
	}


	public String eventModificationPage(){
	
		return "eventModificationPage";
	
	}

	public String calendarPage(){
		return "calendarPage";
	}


	public List<Event> getEventsCreated() {
		return this.eventsCreated;
	}

	public void setEventsCreated(List<Event> eventsCreated) {
		this.eventsCreated = eventsCreated;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDuration() {
		return duration;
	}

	public void setDuration(Date duration) {
		this.duration = duration;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public boolean isOutdoor() {
		return outdoor;
	}

	public void setOutdoor(boolean outdoor) {
		this.outdoor = outdoor;
	}

	public boolean isPublicVisibility() {
		return publicVisibility;
	}

	public void setPublicVisibility(boolean publicVisibility) {
		this.publicVisibility = publicVisibility;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getOrganizers() {
		return organizers;
	}

	public void setOrganizers(String organizers) {
		this.organizers = organizers;
	}

	public String getInvites() {
		return invites;
	}

	public void setInvites(String invites) {
		this.invites = invites;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public Double getTemperature() {
		return temperature;
	}

	public void setTemperature(Double temperature) {
		this.temperature = temperature;
	}

	
	

}
